#include <QState>
#include "giftpixmap.h"
#include <math.h>
#include "animation.h"

IMPLEMENT_SINGLTON_OBJECT(LoveAnimation, getLoveAnimation) ;

void LoveAnimation::generateLoveState(QState *state, Pixmap *item, int radius, int i)
{
	radius /= 2 ;

	switch( i / 16 )
	{
	case 0:	//L
		drawLLetter( state, item, radius, i);
		break ;

	case 1://O
		drawOLetter( state, item, radius, i);
		break ;

	case 2://V
		drawVLetter( state, item, radius, i);
		break ;

	case 3://E
		drawELetter( state, item, radius, i);
		break ;
	}
}

void LoveAnimation::drawLLetter( QState *state, Pixmap *item, int radius, int i)
{//L
	if( i < 10 )
	{
		int index = i / 2 ;

		state->assignProperty(item, "pos",
			QPointF(-radius+(i%2)*20-11*radius/4,20-radius+(index / 5.0)*radius*2));
	}
	else
	{
		int index = i - 10 ;
		state->assignProperty(item, "pos",
			QPointF(-radius+(index/2+2)*20-11*radius/4,20-radius+(3.0  + index%2)/5.0*radius*2));
	}
}

void LoveAnimation::drawOLetter( QState *state, Pixmap *item, int radius, int i)
{//O
	state->assignProperty(item, "pos",
		QPointF(cos(((i-16) / 15.0) * 6.28) * radius - 5 * radius / 4,
		sin(((i-16) / 15.0) * 6.28) * radius));
}

void LoveAnimation::drawVLetter( QState *state, Pixmap *item, int radius, int i)
{//V
	int unit = 40;
	i = i - 32 ;

	if( i < 8 )
	{
		int j = i / 2 ;

		state->assignProperty(item, "pos",
			QPointF(-radius+j*20+5*radius/4,-radius+(j + i % 2)*unit));
	}
	else
	{
		i -= 8 ;
		int j = i / 2 ;

		state->assignProperty(item, "pos",
			QPointF(-radius+5*radius/4+(j+4)*20,-radius-(i % 2 + j - 4)*unit));
	}
}

void LoveAnimation::drawELetter( QState *state, Pixmap *item, int radius, int i)
{//E
	int horizontal = radius * 2 / 5.0 ;
	i -= 48 ;
	if( i < 10 )
	{
		int index = i / 2 ;

		state->assignProperty(item, "pos",
			QPointF(-radius+(i%2)*20+7*radius/2,20-radius+index*horizontal));
	}
	else
	{
		int index = i - 10 ;
		state->assignProperty(item, "pos",
			QPointF(-radius+(index%2+2)*20+7*radius/2,20-radius+index/2*horizontal*2));
	}
}